<?php 
/**
 * Order Form controller
 *
 * @category   Gt
 * @package    Gt_Pos
 * @author     Gentotech <http://www.gentotech.com/>
 */
 
 class Gt_Pos_Adminhtml_Pos_OrderController extends Mage_Adminhtml_Controller_Action {
	protected function _initAction() {
        $this->loadLayout()->_setActiveMenu('pos');
        return $this;
    }
 
	public function indexAction() {
        $this->_title($this->__('Point Of Sale'));
        $this->_initAction();        
        $this->renderLayout();
    }
	
	// public function gridAction()
	// {
		// $this->_addContent($this->getLayout()->createBlock('pos/adminhtml_order_grid'));
		// // $this->getResponse()->setBody(
            // // $this->getLayout()->createBlock('pos/adminhtml_order_grid')->toHtml()
        // // );
		 // $this->_initAction();        
        // $this->renderLayout();
	// }
	
	public function customerGridAction()
	{
		
		$this->getResponse()->setBody(
            $this->getLayout()->createBlock('pos/adminhtml_order_customergrid_grid')->toHtml()
        );
	
	}
	
	public function productGridAction()
	{
		
		$this->getResponse()->setBody(
            $this->getLayout()->createBlock('pos/adminhtml_order_productgrid_grid')->toHtml()
        );
	
	}
	
	public function changeCalculatorAction()
	{
		
		$this->getResponse()->setBody(
            $this->getLayout()->createBlock('pos/adminhtml_order_changecalculator')->toHtml()
        );
		// $this->_initAction();        
        // $this->renderLayout();
	
	}
	
	/**
     * Retrieve session object
     *
     * @return Mage_Adminhtml_Model_Session_Quote
     */
    protected function _getSession()
    {
        return Mage::getSingleton('adminhtml/session_quote');
    }

    /**
     * Retrieve quote object
     *
     * @return Mage_Sales_Model_Quote
     */
    protected function _getQuote()
    {
        return $this->_getSession()->getQuote();
    }
	
	/**
     * Initialize order creation session data
     *
     * @return Mage_Adminhtml_Sales_Order_CreateController
     */
    protected function _initSession()
    {
        /**
         * Identify customer
         */
        if ($customerId = $this->getRequest()->getParam('customer_id')) {
            $this->_getSession()->setCustomerId((int) $customerId);
        }

        /**
         * Identify store
         */
        if ($storeId = $this->getRequest()->getParam('store_id')) {
            $this->_getSession()->setStoreId((int) $storeId);
        }		

        /**
         * Identify currency
         */
        if ($currencyId = $this->getRequest()->getParam('currency_id')) {
            $this->_getSession()->setCurrencyId((string) $currencyId);
            $this->_getOrderCreateModel()->setRecollect(true);
        }
        return $this;
    }
	
	public function shippingMethodAction()
	{
		$this->getResponse()->setBody(
            $this->getLayout()->createBlock('pos/adminhtml_order_shipping_method')->toHtml()
        );
       
	
	}
	
	public function barcodeAction()
	{	
		$productImage = '';
		$result = Array();
		$barcode = $this->getRequest()->getParam('barcode');
		$product = Mage::getModel('catalog/product')->loadByAttribute('barcode',$barcode);
		if($product){
			$result['data'] = Array();
			if($product->getSmallImage() != null && $product->getSmallImage() != "no_selection")
				$productImage = trim(Mage::helper('catalog/image')->init($product, 'small_image')->resize(135));
			else
				$productImage = "";
			
			
			$result['data']['id'] = $product->getId();
			$result['data']['sku'] = $product->getSKU();
			$result['data']['name'] = $product->getName();
			$result['data']['image'] = $productImage;
			$result['data']['price'] = $product->getFinalPrice();	
		}else{
			$result['error_message'] = "Invalid product!";
		}
			
		echo Mage::helper('core')->jsonEncode($result);
	}
	
	public function addnewcustomerAction()
	{	
		$data = $this->getRequest()->getPost();
		//var_dump($data);
		$email = $this->getRequest()->getParam('email');
		$first = $this->getRequest()->getParam('fisrt');
		$last = $this->getRequest()->getParam('last');
		$real = $this->getRequest()->getParam('real');
		//var_dump($email);
		$customer = Mage::getModel('customer/customer');
		//$customer  = new Mage_Customer_Model_Customer();
		 
		 
		$password = '1234567890';
		
		 
		$customer->setWebsiteId(1);
		$customer->loadByEmail($email);
		//Zend_Debug::dump($customer->debug()); exit;
		 
		if(!$customer->getId()) {
		 
			$customer->setEmail($email);
			$customer->setFirstname($first);
			$customer->setLastname($last);
			$customer->setPassword($password);
			
		}
		 
		try {
			//$customer->save();
			$customer->setConfirmation(null);
			$customer->save();
			
			$resource = Mage::getSingleton('core/resource');
			$writeConnection = $resource->getConnection('core_write');
			$query = 'INSERT INTO customer_entity_varchar (entity_type_id,attribute_id,entity_id,`value`) VALUES (1,140,'.$customer->getId().',"'.$real.'")';
			$new_record = $writeConnection->query($query);
			//Make a "login" of new customer			
		}
		 
		catch (Exception $ex) {
			//Zend_Debug::dump($ex->getMessage());
		}   
		$_custom_address = array (
			'firstname' => $customer->getFirstname(),
			'lastname' => $customer->getLastname(),
			'street' => array (
				'0' => 'n/a',
				'1' => 'n/a',
			),		 
			'city' => 'Singapore',
			'region_id' => '',
			'region' => '',
			'postcode' => '31000',
			'country_id' => 'SG', /* Croatia */
			'telephone' => '0038531555444',
		);
		$customAddress = Mage::getModel('customer/address');
		//$customAddress = new Mage_Customer_Model_Address();
		$customAddress->setData($_custom_address)
					->setCustomerId($customer->getId())
					->setIsDefaultBilling('1')
					->setIsDefaultShipping('1')
					->setSaveInAddressBook('1');
		 
		try {
			$customAddress->save();
		}
		catch (Exception $ex) {
			//Zend_Debug::dump($ex->getMessage());
		}
		$result = Array();
		$result['id'] = $customer->getId();
		$result['name'] = $customer->getFirstname().' '.$customer->getLastname();
		$result['groupid'] = $customer->getGroupId();
		$group = Mage::getModel('customer/group')->load($customer->getGroupId());
		$result['groupname'] = $group->getCode();
		
		echo Mage::helper('core')->jsonEncode($result);
	}
 }


?> 
